package com.atguigu.admin.controller;

import com.atguigu.admin.service.ProductService;
import com.atguigu.admin.utils.AliyunOSSUtils;
import com.atguigu.param.ProductSaveParam;
import com.atguigu.param.ProductSearchParam;
import com.atguigu.pojo.Product;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @GetMapping("list")
    public R list(ProductSearchParam productSearchParam){
        return productService.list(productSearchParam);
    }

    // 阿里云图片上传
    @PostMapping("upload")
    public R upload(MultipartFile img) throws Exception {

        String filename = img.getOriginalFilename();
        String contentType = img.getContentType();
        byte[] content = img.getBytes();

        long millis = System.currentTimeMillis();
        filename=millis+filename;

        String url = aliyunOSSUtils.uploadImage(filename, content, contentType, 1000);
        System.out.println("url="+url);
        return R.ok("上传成功",url);

    }

    @PostMapping("save")
    public R save(ProductSaveParam productSaveParam){
        return productService.save(productSaveParam);
    }

    @PostMapping("update")
    public R update(Product product){
        return productService.update(product);
    }

    @PostMapping("remove")
    public R remove(Integer productId){
        return productService.remove(productId);
    }
}
